<?php

/**
 * @author dr.emi
 * @copyright 2010
 */



function konvert_tgl($tgl) {
	$tanggal=substr($tgl,8,2);
	$bulan=substr($tgl,5,2);
	$tahun=substr($tgl,0,4);
	return $tanggal."-".$bulan."-".$tahun ;

}

function konvert_tgl2($tgl) {
	$tanggal=substr($tgl,8,2);
	$bulan=substr($tgl,5,2);
	$tahun=substr($tgl,0,4);
	return $tahun."-".$bulan."-".$tanggal;

}

function tgl_english($tgl){
	$tanggal=substr($tgl,0,2);
	$bulan=substr($tgl,3,2);
	$tahun=substr($tgl,6,4);
	return $tahun."-".$bulan."-".$tanggal ;
}

function tgl_indonesia($tgl) {
	$tanggal=substr($tgl,8,2);
	$bulan=ambil_bulan(substr($tgl,5,2));
	$tahun=substr($tgl,0,4);
	return $tanggal." ".$bulan." ".$tahun ;
}

function ambil_bulan($bln) {
	switch($bln) {
		case 1 : $nama_bulan = "Januari" ; break;
		case 2 : $nama_bulan = "Februari" ; break;
		case 3 : $nama_bulan = "Maret" ; break;
		case 4 : $nama_bulan = "April" ; break;
		case 5 : $nama_bulan = "Mei" ; break;
		case 6 : $nama_bulan = "Juni" ; break;
		case 7 : $nama_bulan = "Juli" ; break;
		case 8 : $nama_bulan = "Agustus" ; break;
		case 9 : $nama_bulan = "September" ; break;
		case 10 : $nama_bulan = "Oktober" ; break;
		case 11 : $nama_bulan = "November" ; break;
		case 12 : $nama_bulan = "Desember" ; break;
	}
	return $nama_bulan ;
}

function get_menu_page(){
	$_this = & get_Instance();
	$sql="SELECT * FROM tbl_page where slug not in('contact-us') ORDER BY id_page ASC";
		//echo $sql;
	$query=$_this->db->query($sql);
	if ($query->num_rows()>0){
		$menu= $query->result_array();
	}else{
		$menu= array();
	}
	
	$str_menu="";
	foreach($menu as $val){
		$str_menu.="<li><a href=".site_url('page/'.$val['slug']).">".$val['judul']."</a></li>";
	}
	return $str_menu;
}

function get_tree_menu(){
	$_this = & get_Instance();
	$sql="SELECT id_produk,id_parent,nama_menu,slug FROM tbl_produk ORDER BY id_produk ASC";
		//echo $sql;
	$query=$_this->db->query($sql);
	if ($query->num_rows()>0){
		$menu= $query->result_array();
	}else{
		$menu= array();
	}
	
	if(count($menu)>0){
		foreach($menu as $val){
			$data[$val['id_parent']][] = $val;
		}
	}
	//echo "<pre>";
	//print_r($data);
	$hasil_menu = get_menu($data);
	return $hasil_menu;
}

function get_menu($data, $parent = 0) {
	static $i = 1;
	$tab = str_repeat("\t\t", $i);
	//echo $data[$parent];
	
	if (isset($data[$parent])) {
		//echo $i;
		if($i==1){
		$html = "";
		}else{
			$html = "<ul class='menu_submenu_block'>";
		}
		$i++;
		foreach ($data[$parent] as $v) {
			$child = get_menu($data, $v['id_produk']);
			
			if ($child) {
				//echo 'a '.$child;
				$i--;
				$html .= "<li class='has-submenu'>";
				$html .= '<a href=#>'.$v['nama_menu'].'</a>';
				$html .= $child;
			}else{
				$html .= "\n\t$tab<li >";
				$html .= '<a href="'.site_url('produk'."/".$v['slug']).'">'.$v['nama_menu'].'</a>';
				$html .= $child;
			}
			$html .= '</li>';
		}
		$html .= "</ul>";
		return $html;
	} else {
		//echo 'salah '.$html;
		return false;
	}
}

function jumlah_minggu($date1,$date2) {
	// memecah bagian-bagian dari tanggal $date1
	$pecahTgl1 = explode("-", $date1);
	// membaca bagian-bagian dari $date1
	$tgl1 = $pecahTgl1[0];
	$bln1 = $pecahTgl1[1];
	$thn1 = $pecahTgl1[2];
	// counter looping
	$i = 0;
	// counter untuk jumlah hari minggu
	$sum = 0;
	do
	{
	// mengenerate tanggal berikutnya
	$tanggal = date("d-m-Y", mktime(0, 0, 0, $bln1, $tgl1+$i, $thn1));
	// cek jika harinya minggu, maka counter $sum bertambah satu, lalu tampilkan tanggalnya
	if (date("w", mktime(0, 0, 0, $bln1, $tgl1+$i, $thn1)) == 0)
	{
	$sum++;
	}
	$i++;
	}
	while ($tanggal != $date2);
	return $sum;
}

function romawi($minggu) {
	switch($minggu) {
		case 1 : $romawi = "I" ; break;
		case 2 : $romawi = "II" ; break;
		case 3 : $romawi = "III" ; break;
		case 4 : $romawi = "IV" ; break;
		case 5 : $romawi = "V" ; break;
	}
	return $romawi ;
}

function cek_magang($date1,$date2,$divisi) {
	$_this = & get_Instance();
	$sql="select * from tbl_magang where 
	((tgl_mulai >= '$date1' and tgl_mulai <= '$date2') or
	(tgl_mulai <= '$date1' and tgl_selesai >= '$date2') or
	(tgl_selesai >= '$date1' and tgl_selesai <= '$date2')) and id_divisi = $divisi";
	//echo $sql;
	$query=$_this->db->query($sql);
	if ($query->num_rows()>0){
		$data= $query->result_array();
		$html = "<td class='bg-table-magang' title='";
		foreach ($data as $val) {
			$html .= "$val[asal_peserta] ";
		}
		$html .= "'></td>";
	}else{
		$html = "<td></td>";
	}
	return $html;
}
?>